package my.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import my.jdbc.DbConUtil;
import my.jdbc.JdbcTemplate;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import util.DataExchangeUtil;
import bean.OssDailyTradeInventory;
import bean.OssMonitorTimeInventory;
import bean.Sale;
import bean.StockData;

@Controller
@RequestMapping("liantiao")
public class LianTiaoToolsAction {

	@RequestMapping("index")
	@ResponseBody
	public Object indexAction(String nodeNo) {
		List<Map<String, Object>> resultMapList = new ArrayList<Map<String, Object>>();
		if (StringUtils.isBlank(nodeNo)) {
			return resultMapList;
		}

		// 油站代码转换
		nodeNo = DataExchangeUtil.getAppStationCode(nodeNo);

		// 查询销售数据
		String bf_sale_sql = " SELECT  TOP 1 inserttime FROM BF_SALE WHERE nodeno = '" + nodeNo + "' ORDER BY inserttime DESC ";
		Object[] bfSale = JdbcTemplate.queryFieldInfoByFields(DbConUtil.get162ptCon(), bf_sale_sql, null, DbConUtil.TYPE_NAME_162PT);

		// 查询销售过滤表数据
		String bf_sale_filter_sql = " SELECT  TOP 1  inserttime FROM BF_SALE_FILTER WHERE nodeno = '" + nodeNo + "' ORDER BY inserttime DESC ";
		Object[] bfSaleFilter = JdbcTemplate.queryFieldInfoByFields(DbConUtil.get162ptCon(), bf_sale_filter_sql, null, DbConUtil.TYPE_NAME_162PT);

		// 查询时点库存数据
		String bf_stock_hout_data_sql = " SELECT TOP 1 inserttime FROM  BF_STOCK_HOUT_DATA WHERE nodeno = '" + nodeNo + "' ORDER BY inserttime DESC ";
		Object[] bfStockHoutData = JdbcTemplate.queryFieldInfoByFields(DbConUtil.get162ptCon(), bf_stock_hout_data_sql, null, DbConUtil.TYPE_NAME_162PT);
		// 查询时点库存过滤数据
		String bf_stock_hout_data_filter_sql = " SELECT TOP 1 inserttime FROM  BF_STOCK_HOUT_DATA_FILTER WHERE nodeno = '" + nodeNo + "' ORDER BY inserttime DESC ";
		Object[] bfStockHoutDataFilter = JdbcTemplate.queryFieldInfoByFields(DbConUtil.get162ptCon(), bf_stock_hout_data_filter_sql, null, DbConUtil.TYPE_NAME_162PT);

		boolean isableSync = (bfSale == null || bfStockHoutData == null) ? true : false;
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("bfSale", bfSale == null ? 0 : 1 + "[" + bfSale[0] + "]");
		resultMap.put("bfSaleFilter", bfSaleFilter == null ? 0 : 1 + "[" + bfSaleFilter[0] + "]");
		resultMap.put("bfStockHoutData", bfStockHoutData == null ? 0 : 1 + "[" + bfStockHoutData[0] + "]");
		resultMap.put("bfStockHoutDataFilter", bfStockHoutDataFilter == null ? 0 : 1 + "[" + bfStockHoutDataFilter[0] + "]");
		resultMap.put("isableSync", isableSync);
		resultMapList.add(resultMap);
		return resultMapList;
	}

	@RequestMapping("sync")
	@ResponseBody
	public Object uploadAndSyncAction(String ipAddress, String nodeNo, String bfSale, String bfStockHoutData) {
		if (StringUtils.isBlank(ipAddress) || StringUtils.isBlank(nodeNo)) {
			return false;
		}
		// 查询该站下的销售和时点库存
		String sale_sql = " SELECT * FROM oss_daily_TradeInventory ORDER BY StockDate DESC LIMIT 1 ";
		OssDailyTradeInventory sale = JdbcTemplate.queryObjectInfoByFields(DbConUtil.getGsmappCon(ipAddress), sale_sql, OssDailyTradeInventory.class, null, ipAddress);

		String sdstock_sql = " SELECT * FROM oss_monitor_TimeInventory  ORDER BY StoreTime DESC  LIMIT 1 ";
		OssMonitorTimeInventory sdstock = JdbcTemplate.queryObjectInfoByFields(DbConUtil.getGsmappCon(ipAddress), sdstock_sql, OssMonitorTimeInventory.class, null, ipAddress);

		// 将数据插入到中转表中
		String insert_sale_sql = " INSERT INTO oss_daily_TradeInventory VALUES([macNo],[tTC],[takeDate],[oilGun],[oilCan],[oilNo],[opeTime],[stockDate],[stockTime],[oilL],[standardL],[emptyL],[heightTotal],[heightWater],[oilTemp],[waterL],[density],[densityStandard],[shift],[remark],[tranStatus],[backCanFlag],[cardNo]) ";
		String insert_sdstock_sql = " INSERT INTO oss_monitor_TimeInventory VALUES([oilCan],[oilNo],[storeTime],[standardL],[heightTotal],[oilL],[heightWater],[waterL],[temperature],[volumeEmpty],[temp1],[temp2],[temp3],[temp4],[temp5],[densities],[densitiesStandard],[tranStatus],[createTime],[shift]) ";

		if (sale != null && bfSale.equals("0")) {
			JdbcTemplate.insertByBean(DbConUtil.get162testCon(), insert_sale_sql, sale, DbConUtil.TYPE_NAME_162TEST);
		}
		if (sdstock != null && bfStockHoutData.equals("0")) {
			JdbcTemplate.insertByBean(DbConUtil.get162testCon(), insert_sdstock_sql, sdstock, DbConUtil.TYPE_NAME_162TEST);
		}

		// 调用接口将数据传递过去

		if (sale != null && bfSale.equals("0")) {
			// 销售数据
			final List<Sale> saleJsonList = new ArrayList<Sale>();
			Sale saleJson = new Sale();
			saleJson.setNodeno(nodeNo);
			saleJson.setMacno(sale.getMacNo());
			saleJson.setTtc(sale.gettTC());
			saleJson.setOilgun(sale.getOilGun());
			saleJson.setOilcan(sale.getOilCan() + "");
			saleJson.setOilno(sale.getOilNo());
			// 泵码
			saleJson.setPumpno("");
			saleJson.setTakedate(sale.getTakeDate());
			saleJson.setOpetime(sale.getOpeTime());
			saleJson.setStockdate(sale.getStockDate());
			saleJson.setStocktime(sale.getStockTime());
			// 加油升数
			saleJson.setLiter(0.0);
			// 回灌标记
			saleJson.setCheckmode("");
			saleJson.setHeighttotal(sale.getHeightTotal());
			saleJson.setHeightwater(sale.getHeightWater());
			saleJson.setOill(sale.getOilL());
			saleJson.setStandardl(sale.getStandardL());
			saleJson.setEmptyl(sale.getEmptyL());
			saleJson.setWaterl(sale.getWaterL());
			saleJson.setOiltemp(sale.getOilTemp());
			saleJson.setDensity(sale.getDensity());
			saleJson.setDensitystandard(sale.getDensityStandard());
			saleJson.setShift(sale.getShift());
			saleJson.setRemark(sale.getRemark());
			saleJsonList.add(saleJson);

			new Thread() {
				@Override
				public void run() {
					DataExchangeUtil.sendSaleData(saleJsonList);
				}
			}.start();
		}

		if (sdstock != null && bfStockHoutData.equals("0")) {
			// 时点库存数据
			final List<StockData> sdStockJsonList = new ArrayList<StockData>();
			StockData sdstockJson = new StockData();
			sdstockJson.setNodeno(nodeNo);
			sdstockJson.setOilcan(sdstock.getOilCan() + "");
			sdstockJson.setOilno(sdstock.getOilNo());
			sdstockJson.setStoretime(sdstock.getStoreTime());
			sdstockJson.setStandardl(sdstock.getStandardL());
			sdstockJson.setHeighttotal(sdstock.getHeightTotal());
			sdstockJson.setOill(sdstock.getOilL());
			sdstockJson.setHeightwater(sdstock.getHeightWater());
			sdstockJson.setWaterl(sdstock.getWaterL());
			sdstockJson.setTemperature(sdstock.getTemperature());
			sdstockJson.setVolumeempty(sdstock.getVolumeEmpty());
			sdstockJson.setTemp1(sdstock.getTemp1());
			sdstockJson.setTemp2(sdstock.getTemp2());
			sdstockJson.setTemp3(sdstock.getTemp3());
			sdstockJson.setTemp4(sdstock.getTemp4());
			sdstockJson.setTemp5(sdstock.getTemp5());
			sdstockJson.setDensities(sdstock.getDensities());
			sdstockJson.setDensitiesstandard(sdstock.getDensitiesStandard());
			sdstockJson.setCreatetime(sdstock.getCreateTime());
			sdstockJson.setShift(sdstock.getShift());
			// 容积表版本号
			sdstockJson.setVersion("");
			sdstockJson.setTranstatus(sdstock.getTranStatus());
			sdStockJsonList.add(sdstockJson);

			new Thread() {
				@Override
				public void run() {
					DataExchangeUtil.sendSdStockData(sdStockJsonList);
				}

			}.start();
		}
		return true;

	}
}
